<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LunboController extends Controller
{
    //添加轮播信息
    public function getAdd()
    {
    	$cates = self::CateList();
        //解析模板
    	return view('admin.lunbo.add',['cates'=>$cates]);
    }
    static public function CateList()
    {
        //分类查询
        $cates = DB::select('select *,concat(path,",",id) as paths from cate order by paths');
        foreach($cates as $k=>$v){
            //把字符串转换成数组
            $arr = explode(',',$v->path);
            //获取长度
            $len = count($arr)-1;
            //重复str_repeat('|---',4);重复4次
            $cates[$k]->name = str_repeat('|----',$len).$v->name;
            // var_dump($len);
        }
        return $cates;
    }

    //执行添加
    public function postInsert(Request $request)
    {
    	//判断数据
        // dd($request->all());
    	 $this->Validate($request, [
            'title' => 'required|max:20',
        ],[
        	'title.required'=>'轮播标题不能为空',
        	'title.max'=>'轮播标题不能超过20个字符'
        ]);
        //获取后缀名
        $su = $request->file('pic')->getClientOriginalExtension();
        $arr = array('jpg','png','jpeg','tiff','gif');
        //判断
        if(!in_array($su,$arr)){
            // die('非法上传');
            echo '<script type="text/javascript">
                alert("上传文件不合法");
                location.href="../lunbo/add";
            </script>';
        }
        //随机文件名
        $name = md5(time()+rand(1,999999));
        //将文件移动到指定目录下
        $request->file('pic')->move('./lunboupload/', $name.'.'.$su);
    
        $arr = $request->only(['title','status','cid']);
        $arr['pic'] = $name.'.'.$su;
        // var_dump($arr);exit;
        //添加
       $res = DB::table('lunbo')->insert($arr);

       //判断添加是否成功
       if($res){
            return redirect('/admin/lunbo/index')->with('success','轮播信息添加成功');
       }else{
            return back()->with('error','轮播信息添加失败');
       }
    }

    //轮播信息列表
    public function getIndex(Request $request)
    {
        // dd($request->all());
        $lunbos = DB::table('lunbo')->get();
        //查询所属分类
        $cates = self::CateList();
        // dd($lunbos);
        //获取 搜索
        $num = $request->input('num',10);
        if($request->input('keywords')){
            $lunbos = DB::table('lunbo')->where('title','like','%'.$request->input('keywords').'%')->paginate($num);   
        }else{
            $lunbos = DB::table('lunbo')->paginate($num);
        }
        $data = $request->all();
        // var_dump($data);
        // dd($data);
        return view('Admin.lunbo.index',['lunbos'=>$lunbos,'request'=>$data,'cates'=>$cates]);
    }

    //ajax删除
    public function postDelete(Request $request)
    {
        // dd($request->all());
        $id = $request->input('id');
        $res = DB::table('lunbo')->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }

    //修改
    public function getEdit($id)
    {
        //查询
        $lunbos = DB::table('lunbo')->where('id',$id)->first();
        //查询所属分类
        $cates = self::CateList();
        // dd($lunbos);
        //解析模板
        return view('admin.lunbo.edit',['lunbos'=> $lunbos,'cates'=>$cates]);
    }

    //执行修改
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        // dd($request->hasFile('pic'));
        $this->Validate($request, [
            'title' => 'required|max:20',
        ],[
            'title.required'=>'轮播标题不能为空',
            'title.max'=>'轮播标题不能超过20个字符'
        ]);
        $id = $request->only('id');
        $data = $request->only(['title','status','cid']);
        //检测文件是否上传
        if($request->hasFile('pic')){
            //如果上传了
            //获取后缀名
            $su = $request->file('pic')->getClientOriginalExtension();
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="../lunbo/edit";
                </script>';
            }
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //将文件移动到指定目录下
            $request->file('pic')->move('./lunboupload/', $name.'.'.$su);
            $data['pic'] = $name.'.'.$su;
        }else{
            $data['pic'] = $request->pic;
        }
        // dd($data);
        //数据库修改
        $res = DB::table('lunbo')->where('id',$id)->update($data);
        // dd($res);

        //判断
        if($res){
            return redirect('/admin/lunbo/index')->with('success','轮播信息修改成功');
        }else{
            return back()->with('error','轮播信息未做任何修改');
        }
    }
}
